from time import sleep,ctime
import logging
import threading
logging.basicConfig(level=logging.INFO)
loops = [3,4,5]
#自定义类继承threading模块中的方法直接重写方法
class MyThread(threading.Thread):
    def __init__(self,func,args,name=''):
        threading.Thread.__init__(self)#主动去调thread的初始方法
        self.func=  func
        self.args = args
        self.name = name
    def run(self):   #重写run方法
        self.func(*self.args)
def loop(nloop,nsec):
    logging.info('start loop'+ str(nloop) + 'at' + ctime())
    sleep(nsec)
    logging.info('end loop' + str(nloop) + 'at' + ctime())
def main():
    logging.info('start all loop'  + ctime())
    threads = []
    nloops = range(len(loops))
    for i in nloops:
        t = MyThread(loop,(i,loops[i]),loop.__name__)
        threads.append(t)
    for i in nloops:
        threads[i].start()
    for i in nloops:
        threads[i].join()
    logging.info('end all loop'  + ctime())
if __name__ == '__main__':
    main()

#原语，多线程访问同一个数据（数据会错误），原语中包含锁（解决数据的互斥访问），信号量